package com.fjh.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.fjh.domain.BusCar;
import com.fjh.domain.BusRent;
import com.fjh.domain.BusReturn;
import com.fjh.mapper.BusCarMapper;
import com.fjh.mapper.BusRentMapper;
import com.fjh.mapper.BusReturnMapper;
import com.fjh.service.ReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ReturnServiceImpl implements ReturnService {
    @Autowired
    private BusReturnMapper returnMapper;

    @Autowired
    private BusRentMapper rentMapper;

    @Autowired
    private BusCarMapper carMapper;

    /**
     * 归还车辆
     *
     * @param rentId
     * @return
     */
    @Override
    @Transactional
    public Boolean returnCar(Integer rentId) {
        // 1 通过租车的id 找到租车的人 ,出租的车辆
        BusRent busRent = rentMapper.selectByPrimaryKey(rentId);
        // 2 将出租记录里面的flag 修改为已经归还
        modifyCarRentStatus(busRent);
        // 3  将出租的车辆状态修改为已经归还
        modifyCarStatus(busRent.getNum());
        // 4 新增一条归还记录
        boolean isOk = saveReturnCar(busRent);
        return isOk;
    }

    /**
     * 修改车辆的状态
     *
     * @param num
     */
    private void modifyCarStatus(String num) {
        // 1 判断之前的车辆的状态
        BusCar busCar = carMapper.selectByNum(num);
        if (busCar == null || busCar.getIsRent() == 0) {
            throw new IllegalArgumentException("当前换车的车辆不存在或状态不合法");
        }
        // 2 修改现在的车辆的状态
        busCar.setIsRent(0);
        carMapper.updateByPrimaryKeySelective(busCar);
    }

    /**
     * 修改借车的记录
     *
     * @param busRent
     */
    private void modifyCarRentStatus(BusRent busRent) {
        if (busRent == null || busRent.getFlag() == 2) {
            throw new IllegalArgumentException("借车记录不存在或者该记录已经被归还了");
        }
        busRent.setFlag(2);
        rentMapper.updateByPrimaryKeySelective(busRent);
    }

    private boolean saveReturnCar(BusRent busRent) {
        BusReturn busReturn = new BusReturn();
        busReturn.setNum(busRent.getNum());
        // 每小时1
        Integer rentPrice = busRent.getRentPrice();
        // 计算总金额
        Long totalTime = cal(DateUtil.parse(busRent.getEndTime(), "yyyy-MM-dd"), DateTime.now());
        busReturn.setTotalMoney((int) (rentPrice * totalTime));
        busReturn.setPayMoney(0);
        busReturn.setProblem("");
        busReturn.setUserId(busRent.getUserId());
        busReturn.setReturnTime(DateUtil.formatTime(new Date()));
        busReturn.setRentPrice(busRent.getRentPrice());
        busReturn.setRentId(busRent.getId());

        return returnMapper.insertSelective(busReturn) > 0;
    }


    /**
     * 计算相隔多少时间
     *
     * @param startTime
     * @param returnTime
     * @return
     */
    private Long cal(DateTime startTime, DateTime returnTime) {
        return DateUtil.between(startTime, returnTime, DateUnit.HOUR);
    }
}
